*************************************************
*												*
*		Intraindividual dynamics in political 	*
*		involvement	and recption				*
*												*
*************************************************

* Author: Maria Preißinger, Alexander Wuttke

					
*------------------------------------------------------------------------------
* Mean values of all reception and interest variables 
*-------------------------------------------------------------------------------

/* Time is considered as calendar weeks before the election */

* Every single wave
foreach v in welec $knowledge	$candidates	$parties	 {
	egen mean`v'all = rowmean(`v'5 `v'6 `v'7 `v'8 `v'9)
}


* waves before NS 
foreach v in welec	$knowledge	$candidates	$parties	interest_el_L	{
	egen mean`v'btwel = rowmean(`v'5 `v'6 `v'7 `v'8)
}

*Waves before federal election
foreach v in welec	$campaign_interest	{
	egen mean`v'befBTW = rowmean(`v'5 `v'6 `v'7)
}
		
*-----------------------------------
* Variance of time to election (for fedelection and NS election)
*----------------------------------

* Every single wave
foreach x of numlist 5/9 {
		 gen varwelecall`x' = (welec`x'-meanwelecall)^2 
	 }
egen varwelecall = rowtotal(varwelecall5-varwelecall9), missing 

* waves before NS 
foreach x of numlist 5/8 {
		 gen varwelecbtwel`x' = (welec`x'-meanwelecall)^2 
	 }
egen varwelecbtwel = rowtotal(varwelecall5-varwelecall8), missing 
	 
*Waves before federal election	
foreach x of numlist 5/7 {
		 gen varwelecbefBTW`x' = (welec`x'-meanwelecall)^2 
	 }
egen varwelecbefBTW = rowtotal(varwelecall5-varwelecall7), missing 
	
*------------------------------------------------
* Covariance of interest/reception with time
*--------------------------------------------------	
	
		
* Every single 
	foreach v in $knowledge	$candidates	$parties {
		
			foreach x of numlist $all {
				gen covhwelec_`v'`x' = (welec`x'-meanwelecall) * (`v'`x'-mean`v'all)
			}
			
			egen cov_welec_`v'all = rowtotal(covhwelec_`v'?), missing
			drop covhwelec_`v'*
		
	}


* waves before NS 

	foreach v in $knowledge	$candidates	$parties	interest_el_L {
		
			foreach x of numlist $btwel {
				gen covhwelec_`v'`x' = (welec`x'-meanwelecbtwel) * (`v'`x'-mean`v'btwel)
			}
			
			egen cov_welec_`v'btwel = rowtotal(covhwelec_`v'?), missing
			drop covhwelec_`v'*
		
	}


*Waves before federal election


	foreach x of numlist 5/7 {
				gen covhwelec_mean_interest_el_B`x' = (welec`x'-meanwelecbefBTW) * (interest_el_B`x'-meaninterest_el_BbefBTW)
				gen covhwelec_mean_interest_el_L`x' = (welec`x'-meanwelecbefBTW) * (interest_el_L`x'-meaninterest_el_LbefBTW)
	}
			
			egen cov_welec_interest_el_BbefBTW = rowtotal(covhwelec_mean_interest_el_B?), missing
			drop covhwelec_mean_interest_el_B?
			egen cov_welec_interest_el_LbefBTW = rowtotal(covhwelec_mean_interest_el_L?), missing
			drop covhwelec_mean_interest_el_L?




*--------------------------------
* Regression coefficients
*---------------------------------						
	foreach p in all btwel befBTW {
		foreach v in 	$knowledge	$candidates	$parties 	interest_el_L interest_el_B {
			capture gen 	c_welec_`v'`p' 		= cov_welec_`v'`p' / varwelec`p'		  //this is the regression coefficient				
			
			capture gen 	ckat_welec_`v'`p'	= -1	if c_welec_`v'`p'<0						//this is a categorial variable, that differs between: negative and positive coefficients and coefficients=0
			capture replace	ckat_welec_`v'`p'	= 0		if c_welec_`v'`p'==0
			capture replace	ckat_welec_`v'`p'	= 1		if c_welec_`v'`p'>0 & c_welec_`v'`p'!=.
		}
	}
		
	


*----------------------------
* Data hygiene
*-----------------------------

drop cov_wel* varwele* meanwele* 


*-------------------------------------------
* Remove persons with only 1 observation
*-------------------------------------------
*gen teilnahme_all	=	w5a + w6a + w7a + w8a + w9a
/*


foreach var in $knowledge	$candidates	$parties 	$campaign_interest {
capture egen help_`var'all 						= rownonmiss(`var'5 `var'6 `var'7 `var'8 `var'9)
capture replace c_welec_`var'all 			= . if help_`var'all == 1
capture replace ckat_welec_`var'all 		= . if help_`var'all == 1
capture  drop  help_`var'all

capture egen help_`var'btwel 			= rownonmiss(`var'5 `var'6 `var'7 `var'8)
capture replace c_welec_`var'btwel		= . if help_`var'all == 1
capture replace ckat_welec_`var'btwel		= . if help_`var'all == 1
capture drop  help_`var'btwel

capture egen help_`var'befBTW 			= rownonmiss(`var'5 `var'6 `var'7)
capture replace c_welec_`var'befBTW 		= . if help_`var'all == 1
capture replace ckat_welec_`var'befBTW	= . if help_`var'all == 1
capture drop  help_`var'befBTW 

}
*/

foreach var in $knowledge	$candidates	$parties	 {
 egen help_`var'all 			= rownonmiss(`var'5 `var'6 `var'7 `var'8 `var'9)
 replace c_welec_`var'all 		= . if help_`var'all == 1	
 replace ckat_welec_`var'all 	= . if help_`var'all == 1
 fre help_`var'all 
  drop  help_`var'all

}

foreach var in $knowledge	$candidates	$parties	interest_el_L {
 egen help_`var'btwel 			= rownonmiss(`var'5 `var'6 `var'7 `var'8)
 replace c_welec_`var'btwel		= . if help_`var'btwel  == 1
 replace ckat_welec_`var'btwel	= . if help_`var'btwel  == 1
 drop  help_`var'btwel

}


foreach var in $campaign_interest	 {
 egen help_`var'befBTW 			= rownonmiss(`var'5 `var'6 `var'7)
 replace c_welec_`var'befBTW 	= . if help_`var'befBTW 	 == 1
 replace ckat_welec_`var'befBTW	= . if help_`var'befBTW 	 == 1
 drop  help_`var'befBTW 
}
*-------------------------------------------
* Label
*-------------------------------------------

lab define ckat 			///
	-1 		"decrease" 		///
	0 		"stable" 		///
	1		"increase"
lab val ckat* ckat	

	
*-------------------------------------------
* Difference between regional and federal election
//Wenn Entwicklung auf der einen Variable, aber nicht auf der anderen, dann gilt es als "nicht gleichläufig"
*-------------------------------------------

lab define dyn_B_L 					///
	-1 		"nicht gleichläufig" 	///
	0 		"beides stabil" 				///
	1		"gleichäufig"

*todo: voting intention

//Candidates
foreach party in cdu spd fdp gru lin {
	gen 	dyn_B_L_cand`party' 	= .
	replace dyn_B_L_cand`party' 	= 1 if ckat_welec_B_cand`party'all == ckat_welec_L_cand`party'all & 	!missing(ckat_welec_B_cand`party'all) & 	!missing(ckat_welec_L_cand`party'all)	&	ckat_welec_B_cand`party'all != 0
	replace dyn_B_L_cand`party' 	= -1 if ckat_welec_B_cand`party'all != ckat_welec_L_cand`party'all & 	!missing(ckat_welec_B_cand`party'all) & 	!missing(ckat_welec_L_cand`party'all) 	
	replace dyn_B_L_cand`party' 	= 0 if ckat_welec_B_cand`party'all == ckat_welec_L_cand`party'all & 	!missing(ckat_welec_B_cand`party'all) & 	!missing(ckat_welec_L_cand`party'all) 	&	ckat_welec_B_cand`party'all == 0
	}



//Parties
foreach party in cdu spd fdp gru   {
	gen 	dyn_B_L_scal`party' 	= .
	replace dyn_B_L_scal`party' 	= 1 if ckat_welec_B_scal`party'all == ckat_welec_L_scal`party'all & 	!missing(ckat_welec_B_scal`party'all) & 	!missing(ckat_welec_L_scal`party'all)	&	ckat_welec_B_scal`party'all != 0
	replace dyn_B_L_scal`party' 	= -1 if ckat_welec_B_scal`party'all != ckat_welec_L_scal`party'all & 	!missing(ckat_welec_B_scal`party'all) & 	!missing(ckat_welec_L_scal`party'all) 	
	replace dyn_B_L_scal`party' 	= 0 if ckat_welec_B_scal`party'all == ckat_welec_L_scal`party'all & 	!missing(ckat_welec_B_scal`party'all) & 	!missing(ckat_welec_L_scal`party'all) 	&	ckat_welec_B_scal`party'all == 0
	}

lab val dyn_B_L_scal* dyn_B_L_cand* dyn_B_L	

*------------------------------
* Sample identifier
*-------------------------------

/* Sample: Valid obersavtion on all intraindiv regression coefficients*/

/*
egen help = rowmiss($knowledge*	$candidates* $parties*	interest_el_L*) 

gen allvalcoef = help==0
	
drop help
*/
	